/**
 * file:   main_loop_stm32_raw.c
 * author: li xianjing <xianjimli@hotmail.com>
 * brief:  main loop for stm32
 *
 * copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. 
 *
 * this program is distributed in the hope that it will be useful,
 * but without any warranty; without even the implied warranty of
 * merchantability or fitness for a particular purpose.  see the
 * license file for more details.
 *
 */

/**
 * history:
 * ================================================================
 * 2018-05-11 li xianjing <xianjimli@hotmail.com> created
 *
 */
#include "stdbool.h"
#include "base/g2d.h"
#include "base/idle.h"
#include "base/timer.h"
#include "lcd/lcd_mem.h"
#include "tkc/mem.h"
#include "base/lcd.h"
#include "lcd/lcd_mem_bgr565.h"
#include "main_loop/main_loop_simple.h"
#include "ST1633E.h"

#include "fsl_common.h"
#include "fsl_elcdif.h"
uint8_t window_line_flag = false;
#define USE_LCD_SYNC 1
extern uint16_t* s_frameBuffer_front;
extern uint16_t* s_frameBuffer_back;
//extern uint16_t s_frameBuffer_front[800*480];
//extern uint16_t s_frameBuffer_back[800*480];
volatile bool s_frameDone = false;

volatile static bool start_swap = false;

/*LCD缓存指针在此处被定义*/
uint8_t platform_disaptch_input(main_loop_t* loop) {	
	static bool touched = false;
	static bool frist = true;	    
	touch_point_t touch_point;
	get_touch_point(&touch_point);
	if(touch_point.touch_status == true)
	{
			if(frist == true)
			{
					/* is frist touch*/
					main_loop_post_pointer_event(loop,TRUE,touch_point.x,touch_point.y);
					frist = false;
					touched = true;
			}else{
					/* touched but not frist*/
					main_loop_post_pointer_event(loop,TRUE,touch_point.x,touch_point.y);
					/* you can add filter code here*/
			}
	}else{
			if(touched == true)
			{
					frist = true;
					touched = false;
					main_loop_post_pointer_event(loop,FALSE,touch_point.x,touch_point.y);
			}else{
					main_loop_post_pointer_event(loop,FALSE,touch_point.x,touch_point.y);
			}
	}
	
	return 0;
}

static ret_t main_loop_dispatch(main_loop_simple_t* loop) {
	
	return RET_OK;
}

lcd_t* lcd = NULL;

#define USING_LCD_SYNC 1
volatile static bool swap_end = false;
ret_t lcd_mem_swap(lcd_t* lcd)
{
    lcd_mem_t* mem = (lcd_mem_t*)lcd;
    uint8_t* temp_fb = mem->offline_fb; 
    
    
    
    return RET_OK;
}

ret_t lcd_mem_sync(lcd_t* lcd)
{
    lcd_mem_t* mem = (lcd_mem_t*)lcd;
    uint8_t* temp_fb = mem->offline_fb;    
    
#if USING_LCD_SYNC
    start_swap = true;
    while(s_frameDone == false){};
    //ELCDIF_RgbModeStop(LCDIF);
    s_frameDone = false;
    start_swap = false;    
#endif
    lcd_mem_set_offline_fb(mem,mem->online_fb);
    lcd_mem_set_online_fb(mem,temp_fb);
    if(window_line_flag == true)
    {
        SDK_DelayAtLeastUs(6000, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);    
        ELCDIF_SetNextBufferAddr(LCDIF,(uint32_t)mem->offline_fb);
    }else{
        ELCDIF_SetNextBufferAddr(LCDIF,(uint32_t)mem->offline_fb);
    }
       
    return RET_OK;
}

//void APP_LCDIF_IRQHandler(void)
//{
//    uint32_t intStatus; 
//    lcd_mem_t* mem = (lcd_mem_t*)lcd;
//    intStatus = ELCDIF_GetInterruptStatus(LCDIF);
//    if(intStatus & kELCDIF_VsyncEdge)
//    {
//#if USING_LCD_SYNC 
//        if(start_swap == true)
//        {
//            s_frameDone = true;
//        }
//        if(swap_end == true)
//        {
//            swap_end = false;
//            ELCDIF_SetNextBufferAddr(LCDIF,(uint32_t)mem->online_fb);    
//        }
//#endif     
//    }
//     ELCDIF_ClearInterruptStatus(LCDIF, intStatus);   

//    SDK_ISR_EXIT_BARRIER;
//}


lcd_t* platform_create_lcd(wh_t w, wh_t h) {
		lcd = lcd_mem_bgr565_create_double_fb(w, h,(uint8_t*)s_frameBuffer_front,(uint8_t*)s_frameBuffer_back);
        //lcd->swap = lcd_mem_swap;
        //lcd->support_dirty_rect = TRUE;
        //lcd->sync = lcd_mem_sync;
        return lcd;
}

#include "main_loop/main_loop_raw.inc"
